<template>
	<view class="container">
		<view class="form">
			<uni-forms ref="formRef" :model="form" :rules="rules" label-position="top">
				<uni-forms-item label="任务名称" name="jobName" required>
					<uni-easyinput v-model="form.jobName" placeholder="请输入任务名称" />
				</uni-forms-item>
				<uni-forms-item label="任务分组" name="jobGroup" required>
					<xy-select v-model="form.jobGroup" :data="sys_job_group" placeholder="任务分组" clearable />
				</uni-forms-item>
				<uni-forms-item label="调用方法" name="invokeTarget" required>
					<uni-easyinput v-model="form.invokeTarget" placeholder="请输入调用目标字符串" />
				</uni-forms-item>
				<uni-forms-item label="cron表达式" name="cronExpression" required>
					<uni-easyinput v-model="form.cronExpression" placeholder="请输入cron表达式" />
				</uni-forms-item>
				<uni-forms-item label="状态" name="cronExpression" required>
					<xy-select v-model="form.status" :data="sys_job_status" placeholder="状态" clearable placement="top" />
				</uni-forms-item>
				<uni-forms-item label="执行策略" name="misfirePolicy" required>
					<uni-data-checkbox v-model="form.misfirePolicy" :localdata="misfirePolicyList" />
				</uni-forms-item>
				<uni-forms-item label="是否并发" name="concurrent" required>
					<uni-data-checkbox v-model="form.concurrent" :localdata="concurrentList" />
				</uni-forms-item>
				<!-- <uni-forms-item label="备注" name="remark">
					<uni-easyinput type="textarea" :maxlength="-1" autoHeight v-model="form.remark" placeholder="请输入备注" />
				</uni-forms-item> -->
				<uni-forms-item>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="submitForm()">确认</button>
					<button class="mini-btn xy-btn" type="warn" size="mini" @click="cancel()">取消</button>
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad,
		onUnload
	} from '@dcloudio/uni-app'
	import { listJob, getJob, delJob, addJob, updateJob, runJob, changeJobStatus } from "@/pages_work/api/monitor/job";
	const {
		proxy
	} = getCurrentInstance();
	const {
		sys_job_group, sys_job_status
	} = proxy.useDict("sys_job_group", "sys_job_status");

	/** 表单数据 */
	const form = ref({});
	
	const misfirePolicyList = ref([{
		text: "立即执行",
		value: "1"
	}, {
		text: "执行一次",
		value: "2"
	}, {
		text: "放弃执行",
		value: "3"
	}]);
	
	const concurrentList = ref([{
		text: "允许",
		value: "0"
	}, {
		text: "禁止",
		value: "1"
	}]);

	/** 表单检验规则 */
	const rules = ref(proxy.uniappRules({
		jobName: [{ required: true, message: "任务名称不能为空", trigger: "blur" }],
		invokeTarget: [{ required: true, message: "调用目标字符串不能为空", trigger: "blur" }],
		cronExpression: [{ required: true, message: "cron执行表达式不能为空", trigger: "change" }]
	}))

	onLoad((params) => {
		uni.setNavigationBarTitle({
			title: params.title
		});
		if (params.jobId == undefined || params.jobId == null || params.jobId == '') {
		} else {
			handleUpdate(params);
		}
	})

	/** 取消按钮 */
	const cancel = () => {
		uni.navigateBack()
	}

	/** 提交按钮 */
	const submitForm = () => {
		proxy.$refs["formRef"].validate().then(valid => {
			if (valid) {
				if (form.value.jobId != undefined) {
					updateJob(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						setTimeout(uni.navigateBack, 1000);
					});
				} else {
					addJob(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						setTimeout(uni.navigateBack, 1000);
					});
				}
			}
		});
	};
	
	/** 修改角色 */
	const handleUpdate = (row) => {
		const id = row.jobId;
		getJob(id).then(response => {
			form.value = response.data;
		});
	}
	
	onUnload(() => {
		uni.$emit('update')
	})
	
</script>

<style lang="scss">
	@import "@/static/scss/work/form.scss";
</style>
